<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>茶叶主页</title>
    <script src="../js/axios.min.js"></script>
</head>
<body>
<a href="./add1.html">添加</a>
<table border="1" cellspacing="0" width="100%" id="teaTbale">

</table>

<script>
    //网页标签加载完后会触发onload事件，调用匿名函数
    window.onload=function (){
        axios.get("/teas").then(function (resp) {
            let teas = resp.data;
            console.log(teas)
            let tableData = " <tr>\n" +
                "        <th>id</th>\n" +
                "        <th>茶叶名称</th>\n" +
                "        <th>茶叶类别</th>\n" +
                "        <th>排序</th>\n" +
                "        <th>描述</th>\n" +
                "        <th>状态</th>\n" +
                "        <th>操作</th>\n" +
                "    </tr>";

            //遍历users
            for (let i = 0; i < teas.length; i++) {
                var tea=teas[i];
                tableData+="\n"+
                    " <tr>\n"+
                    "        <td>"+tea.id+"</td>\n" +
                    "        <td>"+tea.teaName+"</td>\n" +
                    "        <td>"+tea.teaTypeId+"</td>\n" +
                    "        <td>"+tea.ordered+"</td>\n" +
                    "        <td>"+tea.description+"</td>\n" +
                    "        <td>"+tea.status+"</td>\n" +
                    "        <td><button onclick='updateTea("+tea.id+")'>修改</button>&nbsp;&nbsp;&nbsp;" +
                    "<button onclick='deleteTea("+tea.id+")'>删除</button></td>\n" +
                    "    </tr>";
            }
            //设置表格数据
            document.getElementById("teaTbale").innerHTML=tableData;
        })
    }


    function deleteTea(tid) {
        //axios.delete('/teas/'+id+'')
        axios({
            method:'delete',
            url:'/teas/'+tid
        }).then(function (resp) {
            if (resp.data==1){
                location.href='./teas.html'
            }
        })
    }

    /*function updateTea(tid){
        axios({
            method:'get',
            url:'/teas/'+tid
        }).then(function (resp) {
            if (resp.data==1){
                location.href='./teas.html'
            }
        })
    }*/

</script>
</body>
</html>